package com.ourteam.product.requirement.service;

import java.util.Map;

import org.apache.commons.collections.MapUtils;

import com.ourteam.product.requirement.dao.IProductCatalogDAO;
import com.ourteam.product.requirement.dao.ProductCatalog;
import com.ourteam.product.requirement.dao.ProductCatalogDAOQueryBean;
import com.ourteam.product.requirement.dao.ProductDefineDAOQueryBean;
import com.ourteam.product.requirement.domain.ProductCatalogBean;

/**
 *
 * Template Name : 业务服务实现 Template Version : 1 Template Last Modfiy Date : Mon
 * Nov 06 10:49:11 IRKT 2017 Auto Generate Date : 2017-11-23 10:38:03
 *
 */
public class ProductCatalogServiceImpl extends AbstractProductCatalogService {
	/**
	 * 取得产品目录
	 * 
	 * @param aParentId
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductCatalogBean[] doGetProductCatalogs(final long aParentId)
			throws Exception {

		ProductCatalogDAOQueryBean queryBean = new ProductCatalogDAOQueryBean();

		queryBean.createCriteria().andParentIdEqualTo(Long.valueOf(aParentId));

		queryBean.setSortProperty(IProductCatalogDAO.SortIndex);

		ProductCatalog[] productCatalogs = productCatalogDao.query(queryBean);

		return ProductCatalogBean.toArray(productCatalogs);
	}

	/**
	 * 创建产品目录
	 * 
	 * @param aProductCatalog
	 * @throws Exception
	 */
	protected void doCreateProductCatalog(
			final com.ourteam.product.requirement.domain.ProductCatalogBean aProductCatalog) throws Exception {

		ProductCatalog productCatalog = aProductCatalog.convertToProductCatalog();

		if (productCatalogDao.checkIsUnique(productCatalog) == false) {
			throw new Exception("catalog '" + productCatalog.getCatalogName() + "' already exist ");
		}

		productCatalogDao.insert(productCatalog);

	}

	/**
	 * 更新产品目录
	 * 
	 * @param aProductCatalog
	 * @throws Exception
	 */
	protected void doModifyProductCatalog(
			final com.ourteam.product.requirement.domain.ProductCatalogBean aProductCatalog) throws Exception {

		ProductCatalog productCatalog = aProductCatalog.convertToProductCatalog();

		if (productCatalogDao.checkIsUnique(productCatalog) == false) {
			throw new Exception("catalog '" + productCatalog.getCatalogName() + "' already exist ");
		}

		productCatalogDao.update(productCatalog);
	}

	/**
	 * 删除产品目录
	 * 
	 * @param aProductCatalogId
	 * @throws Exception
	 */
	protected void doDeleteProductCatalog(final long aProductCatalogId) throws Exception {

		if (canDeleteProductCatalog(aProductCatalogId)) {
			productCatalogDao.delete(aProductCatalogId);
		} else {

			ProductCatalog productCatalog = productCatalogDao.findById(aProductCatalogId);

			throw new Exception("catalog '" + productCatalog.getCatalogName() + "' is not empty,can not be deleted ");
		}
	}

	/**
	 * 根据ID取得产品目录
	 * 
	 * @param aProductCatalogId
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductCatalogBean doGetProductCatalogById(
			final long aProductCatalogId) throws Exception {

		ProductCatalog productCatalog = productCatalogDao.findById(aProductCatalogId);

		if (productCatalog != null) {
			return new ProductCatalogBean(productCatalog);
		} else {
			return null;
		}
	}

	/**
	 * 取得产品目录是否可以删除
	 * 
	 * @param aProductCatalogId
	 * @throws Exception
	 */
	protected boolean doCanDeleteProductCatalog(final long aProductCatalogId) throws Exception {

		ProductDefineDAOQueryBean queryBean = new ProductDefineDAOQueryBean();

		queryBean.createCriteria().andCatalogIdEqualTo(Long.valueOf(aProductCatalogId));

		int count = productDefineDao.queryCount(queryBean);

		return count == 0;
	}

	/**
	 * 取得产品目录排序
	 * 
	 * @param aParentId
	 * @throws Exception
	 */
	protected int doGetMaxProductCatalogSort(final long aParentId) throws Exception {
		ProductCatalogDAOQueryBean queryBean = new ProductCatalogDAOQueryBean();

		queryBean.createCriteria().andParentIdEqualTo(Long.valueOf(aParentId));

		queryBean.addMaxFunctionProperty(IProductCatalogDAO.SortIndex, "MaxSort");

		Map[] results = productCatalogDao.querySelective(queryBean);

		return MapUtils.getIntValue(results[0], "MaxSort");
	}

	/**
	 * 取得所有产品目录
	 * 
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductCatalogBean[] doGetAllProductCatalogs() throws Exception {
		ProductCatalogDAOQueryBean queryBean = new ProductCatalogDAOQueryBean();

		queryBean.setSortProperty(IProductCatalogDAO.ParentId);

		queryBean.setSortProperty(IProductCatalogDAO.SortIndex);

		ProductCatalog[] productCatalogs = productCatalogDao.query(queryBean);

		return ProductCatalogBean.toArray(productCatalogs);
	}

	@Override
	protected ProductCatalogBean[] doQueryProductCatalogs(ProductCatalogDAOQueryBean queryBean) throws Exception {
		return ProductCatalogBean.toArray(this.productCatalogDao.query(queryBean));
	}

	@Override
	protected int doQueryProductCatalogCount(ProductCatalogDAOQueryBean querBean) throws Exception {
		return this.productCatalogDao.queryCount(querBean);
	}
}
